'use strict';

const Service = require("egg").Service;

const User = require("service/models/User")()
const { unescape } = require("querystring");
const Serialize = require("service/models/Serialize")
        
// 执行数据操作的服务层： 异步调用，异步方法执行  
class DataIndex extends Service{
    async UserSelectFirst(new_paramsObj = null ) {   
        return await this.ctx.app.mysql.get(User.table , new_paramsObj)  
    }
    async UserSelect(new_field = null,new_paramsObj = null, new_paramsStr = null, is_strict = true) {  
        let sql = Serialize.sql_select(User.table,User.fillable,User.hidden,new_field,is_strict) 
        let paramsStr = Serialize.sql_where(new_paramsObj) 
        paramsStr = Serialize.sql_str_default(new_paramsStr,paramsStr)    
        sql = unescape(sql+paramsStr)  // 中文解码 
        return await this.ctx.app.mysql.query(sql)  
    }
    async UserUpdate(new_fieldObj ,new_paramsObj = null, new_paramsStr = null) {  
        let sql = Serialize.sql_update(User.table,new_fieldObj) 
        let paramsStr = Serialize.sql_where(new_paramsObj) 
        paramsStr = Serialize.sql_str_default(new_paramsStr,paramsStr)   
        sql = unescape(sql+paramsStr)  // 中文解码
        return await this.ctx.app.mysql.query(sql )  
    }
    async UserInsert( new_fieldObj , is_strict = true) {  
        let sql = Serialize.sql_insert(User.table,new_fieldObj,is_strict,User.primaryKey) 
        sql = unescape(sql)  // 中文解码
        return await this.ctx.app.mysql.query(sql)  
    }
    async UserDelete(new_paramsObj , is_strict = true) {  
        let sql = Serialize.sql_delete(User.table,is_strict) 
        let paramsStr = Serialize.sql_where(new_paramsObj)   
        sql = unescape(sql+paramsStr)  // 中文解码
        return await this.ctx.app.mysql.query(sql)  
    }
 
}

module.exports = DataIndex;